package com.caishi.lkx.exam.api;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.caishi.lkx.common.AdminBaseApi;
import com.caishi.lkx.common.role.Roles;
import com.caishi.lkx.exam.model.CorrectionModel;
import com.caishi.lkx.exam.service.ICorrectionService;
import com.caishi.lkx.user.service.IUserService;
import com.zzw.common.Wrappers;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author by liyihua
 * @date : 2022-04-01 11:01
 */
@Tag(name = "纠错相关")
@Slf4j
@RestController
@RequestMapping("/exam/correction")
public class CorrectionApi extends AdminBaseApi<ICorrectionService, CorrectionModel, String> {

    @Resource
    private ICorrectionService correctionService;
    @Resource
    private IUserService userService;

    @Override
    public ICorrectionService loadService() {
        return correctionService;
    }

    @Override
    protected List<CorrectionModel> listCover(List<CorrectionModel> result, JSONObject otherParam) {
        result.parallelStream().forEach(r -> {
            if(r.getModifyBy() != null){
                r.setModifyBy(userService.queryName(r.getModifyBy()));
            }
        });
        return result;
    }

    @PostMapping("/update")
    @Override
    @Roles
    public CorrectionModel add(@RequestBody CorrectionModel entity) {
        return super.add(entity);
    }

    @PutMapping("batch-sign")
    @Operation(summary = "批量标记")
    public Boolean batchSign(@RequestParam List<String> ids, @RequestParam Boolean status, @RequestParam(required = false) String desc) {
        CorrectionModel correctionModel = new CorrectionModel();
        correctionModel.setHandle(status);
        if (StrUtil.isNotBlank(desc)) {
            correctionModel.setDescription(desc);
        }
        Integer integer = correctionService.batchUpdate(correctionModel, Wrappers.<CorrectionModel>lambdaUpdate().in(CorrectionModel::getId, ids));
        return integer > 0;
    }

    @DeleteMapping("batch-delete")
    @Operation(summary = "批量删除")
    public Boolean batchDelete(@RequestParam List<String> ids) {
        Integer integer = correctionService.batchUpdate(null, Wrappers.<CorrectionModel>lambdaUpdate().in(CorrectionModel::getId, ids)
                .set(CorrectionModel::getDeleted, true));
        return integer > 0;
    }

}
